import React, { Component } from 'react';
import { connect } from 'react-redux' 
import { baseUrl } from '../../config/system';

class UnityBox extends Component {
    constructor(props) {
        super(props);
    }
    
    componentDidMount() {
        var ws = new SockJS(baseUrl+"/message");
        ws.onopen = function () {
           console.log("onpen");
           ws.send("{}");
        }
        ws.onclose = function () {
            console.log("onclose");
        }
    
        ws.onmessage = function (msg) {
            console.log(msg+"发送消息");
            if(msg.data!="成功建立socket连接"){
                console.log(eval("("+msg.data+")"));
                var datas=eval("("+msg.data+")");
                if(datas.type==1){
                    console.log(datas.type);
                    upData(datas.rackCode,datas.dataList);
                }else if(datas.type==2){
                    downData(datas.rackCode,datas.dataList);
                }else{
                    showAlarmMessageFn(eval("("+msg.data+")"));
                }
              
            }
           
        }
        u = new UnityObject2(this.props.config);
        var $missingScreen = $("#unityPlayer").find(".missing");
        var $brokenScreen = $("#unityPlayer").find(".broken");
        $missingScreen.hide();
        $brokenScreen.hide();
        u.observeProgress(function (progress) {
            switch (progress.pluginStatus) {
                case "broken":
                    $brokenScreen.find("a").click(function (e) {
                        e.stopPropagation();
                        e.preventDefault();
                        u.installPlugin();
                        return false;
                    });
                    $brokenScreen.show();
                    break;
                case "missing":
                    $missingScreen.find("a").click(function (e) {
                        e.stopPropagation();
                        e.preventDefault();
                        u.installPlugin();
                        return false;
                    });
                    $missingScreen.show();
                    break;
                case "installed":
                    $missingScreen.remove();
                    break;
                case "first":
                    break;
            }
        });
        u.initPlugin($("#unityPlayer")[0], "unity/web4.unity3d");
    }

    render() {
        return (
            <div id="unityPlayer">
                <div className="missing">
                    <a href="http://unity3d.com/webplayer/" title="Unity Web Player. Install now!">
                        <img alt="Unity Web Player. Install now!" src="http://webplayer.unity3d.com/installation/getunity.png" width="193" height="63" />
                    </a>
                </div>
                <div className="broken">
                    <a href="http://unity3d.com/webplayer/" title="Unity Web Player. Install now! Restart your browser after install.">
                        <img alt="Unity Web Player. Install now! Restart your browser after install." src="http://webplayer.unity3d.com/installation/getunityrestart.png" width="193" height="63" />
                    </a>
                </div>
            </div>
        );
    }
}

export default UnityBox